package examples;

import com.hyperrealm.kiwi.ui.*;
import com.hyperrealm.kiwi.ui.model.*;
import com.hyperrealm.kiwi.util.*;

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;



public class TableTest extends KFrame
{
  private DomainObjectListModel<TableTest.Person> listModel;
  private PersonFieldAdapter adapter;
  private KTableColumnModel columnModel;
  private KTable table;

  private TableTest()
  {
    super("Table Test");

    adapter = new PersonFieldAdapter();
    listModel = new DomainObjectListModel<TableTest.Person>(adapter);
    columnModel = new KTableColumnModel(adapter);

    table = new KTable();
    table.setSortable(true);

    table.setColumnModel(columnModel);
    table.setModel(new KListModelTableAdapter(listModel));
    KScrollPane sp = new KScrollPane(table);
    sp.setVerticalScrollBarPolicy(
      ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    sp.setHorizontalScrollBarPolicy(
      ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    JPanel p = getMainContainer();
    p.setLayout(new BorderLayout(5, 5));
    p.setBorder(KiwiUtils.defaultBorder);

    TableColumnSelector tsel = new TableColumnSelector(columnModel);

    sp.setCorner(JScrollPane.UPPER_RIGHT_CORNER, tsel);
    
    p.add("Center", sp);

    listModel.addItem(new Person("John", "Smith", 30, "Cornell"));
    listModel.addItem(new Person("Tim", "Doe", 40, "Stanford"));
    listModel.addItem(new Person("Zelda", "Richards", 25, "Yale"));

    setSize(530, 500);
  }

  protected boolean canClose()
  {
    System.exit(0);
    
    return(true);
  }
  
  public static void main(String[] args)
  {
    TableTest test = new TableTest();

    test.setVisible(true);
  }

  private class Person extends DomainObject
  {
    private String firstName, lastName, school;
    private int age;
    
    Person(String firstName, String lastName, int age, String school)
    {
      this.firstName = firstName;
      this.lastName = lastName;
      this.age = age;
      this.school = school;
    }

    String getFirstName()
    {
      return(firstName);
    }

    String getLastName()
    {
      return(lastName);
    }

    int getAge()
    {
      return(age);
    }

    String getSchool()
    {
      return(school);
    }
  }

  private class PersonFieldAdapter implements DomainObjectFieldAdapter<Person>
  {
    private String names[] = new String[] { "Last Name", "First Name", "Age",
                                            "Alma Mater"};
    private int widths[] = new int[] { 200, 200, 100, 150 };
    
    public TableCellEditor getCellEditor(int field)
    {
      return(null);
    }

    public TableCellRenderer getCellRenderer(int field)
    {
      return(null);
    }

    public Object getField(Person object, int field)
    {
      switch(field)
      {
        case 0:
          return(object.getLastName());

        case 1:
          return(object.getFirstName());

        case 2:
          return(object.getAge());

        case 3:
          return(object.getSchool());

        default:
          return(null);
      }
    }

    public Class getFieldClass(int field)
    {
      switch(field)
      {
        case 0:
        case 1:
        case 3:
          return(String.class);
          
        case 2:
          return(Integer.class);
          
        default:
          return(null);
      }
    }

    public int getFieldCount()
    {
      return(names.length);
    }

    public int getFieldMaxWidth(int field)
    {
      return(1000);
    }

    public int getFieldMinWidth(int field)
    {
      return(/*widths[field]*/ 20);
    }


    public int getFieldPreferredWidth(int field)
    {
      return(widths[field]);
    }

    public String getFieldName(int field)
    {
      return(names[field]);
    }

    public boolean isFieldEditable(Person object, int field)
    {
      return(false);
    }

    public void setField(Person object, int field, Object value)
    {
      // not used
    }

    public Icon getIcon(Person object)
    {
      return(null);
    }

    public String getLabel(Person object)
    {
      return(object.toString());
    }
  }
  
}
